#!/usr/bin/python
# -*- coding: utf-8 -*-

from DBA.dbmanager import DBInterface
from CampagneDAC import Campagne

class AccessPoint:
    """
    Classe d acces aux Access Points references dans la base
    Table: PR5QTAP et PR5QTSP
    """    
    def __init__(self,id=""):
        """
        Constructeur
        @param id identificateur de l'AP: soit on le rentre, soit il sera genere automatiquement.
        """
        self.id            = {"key":"ID","value":id}
        self.name        = {"key":"nom","value":""}
        self.essid        = {"key":"essid","value":""}
        self.macaddress        = {"key":"mac","value":""}
        self.xposition        = {"key":"x","value":""}
        self.yposition        = {"key":"y","value":""}
        self.zposition        = {"key":"z","value":""}
        self.mobile        = {"key":"mobile","value":""}
        self.cryptage        = {"key":"cryptage","value":""}
        self.dhcp        = {"key":"dhcp","value":""}
        self.geodescription    = {"key":"geodescription","value":""}
        self.__interfacebase    = DBInterface()
        if not id:
            self.id["value"]    = str(self.__interfacebase.createNewID());
    

    def createByDico(self,dico):
        """
        Creation d'un AP depuis un dictionnaire defini dans l'IHM
        @param dico dictionnaire genere par l'IHM en page4.py
        """
        self.name["value"]         = dico[self.name["key"]]
        self.essid["value"]         = dico[self.essid["key"]]
        self.macaddress["value"]     = dico[self.macaddress["key"]]
        self.xposition["value"]     = dico[self.xposition["key"]]
        self.yposition["value"]     = dico[self.yposition["key"]]
        self.zposition["value"]     = dico[self.zposition["key"]]
        self.mobile["value"]         = dico[self.mobile["key"]]
        self.cryptage["value"]         = dico[self.cryptage["key"]]
        self.dhcp["value"]         = dico[self.dhcp["key"]]
        self.geodescription["value"]     = dico[self.geodescription["key"]]

    def selectCampagneFromDb(self,idSA,idAP):
        """
        Selection d'une campagne en foonction d'une salle et d'un AP
        @param idSA identificateur unique de la salle
        @param idAP identificcateur unique de l'AP
        """
        key ={}
        key["link_sa"] = idSA
        key["link_ap"] = idAP
        return self.__interfacebase.selectTable(self.__interfacebase.ref_AP_Salle_Campagnetablename,self.__interfacebase.ref_AP_Salle_CampagneMapping,key)

    def insertToDb(self,idSalle):
        """
        Insertion d'un AP dans la table
        @param idSalle Identificateur unique de la salle
        """
        dico = self.createAccessPointDico(idSalle)
        self.__interfacebase.insert(self.__interfacebase.accessPointtablename,self.__interfacebase.accessPointmapping,dico)

    def deleteFromDb(self,idSalle):
        """
        Suppression d'un AP dans la table.
        Il est necessaire d'avoir un idSalle pour garder l'integrite de la base de donnee.
        De plus la fonction supprime aussi toutes les campagnes associes a ce couple AP/Salle
        @param idSalle identificateur unique de la salle
        """
        #Criteres pour la supression de campagne
        key={}
        key['link_sa']=idSalle
        key['link_ap']=self.id["value"]
        #Au cas ou il n'y aurai pas de campagne
        elementsuppr=0
        #On recupere les campagnes corresppondantes
        ensCamp=self.selectCampagneFromDb(idSalle,self.id["value"])
        #Pour chaque campagne, on supprimera l association puis la campagne
        for campagne in ensCamp:
            key['ID']=campagne['ID']
            self.__interfacebase.deleteFromTable(self.__interfacebase.ref_AP_Salle_Campagnetablename,self.__interfacebase.ref_AP_Salle_CampagneMapping,key)
            ca = Campagne(campagne['ID'])
            elementsuppr=elementsuppr + int(ca.deleteFromDb())
        #Et on supprime l'AP
        key2={}
        key2['ID']=self.id["value"]
        key2['link_sa']=idSalle
        elementsuppr=elementsuppr + int(self.__interfacebase.deleteFromTable(self.__interfacebase.accessPointtablename,self.__interfacebase.accessPointmapping,key2))
        return elementsuppr
        
    
    def updateIntoDb(self,idSalle):
        """
        Mise a jour d'un AP
        @param idSalle identificateur unique de la salle qui correspond a l'AP
        """
        dico = self.createAccessPointDico(idSalle)
        self.__interfacebase.update(self.__interfacebase.accessPointtablename,self.__interfacebase.accessPointmapping,dico)
    
    def createAccessPointDico(self,idSalle):
        """
        Creation d'un dictionnaire pour access Point.
        @param idSalle identificateur unique associé a la salle ou est l'AP
        """
        dico ={}
        dico[self.id["key"]]        = self.id["value"]
        dico["link_sa"]            = idSalle
        dico[self.name["key"]]        = self.name["value"]
        dico[self.essid["key"]]        = self.essid["value"]
        dico[self.macaddress["key"]]    = self.macaddress["value"]
        dico[self.xposition["key"]]    = self.xposition["value"]
        dico[self.yposition["key"]]    = self.yposition["value"]
        dico[self.zposition["key"]]    = self.zposition["value"]
        dico[self.mobile["key"]]    = self.mobile["value"]
        dico[self.cryptage["key"]]    = self.cryptage["value"]
        dico[self.dhcp["key"]]        = self.dhcp["value"]
        dico[self.geodescription["key"]]= self.geodescription["value"]
        return dico

if __name__=="__main__":
    bat = Batiment()
    bat.name["value"]        = "test";
    bat.description["value"]    = "Description du batiment";
    bat.xposition["value"]        = "34";
    bat.yposition["value"]        = "23";
    bat.zposition["value"]        = "15";
    bat.height["value"]        = "123";
    bat.width["value"]        = "23";
    
    from SalleModule import Salle
    sal = Salle()
    sal.name["value"]        = "salle 1";
    #sal.description["value"]    = "Description";
    sal.xposition["value"]        = "12";
    sal.yposition["value"]        = "12";
    sal.zposition["value"]        = "12";
    sal.height["value"]        = "432";
    sal.width["value"]        = "234";
    bat.addSalle(sal)
    bat.insertToDb();
